<?php

namespace App\Modules\Apicrzs\Services;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Member\UsersIdCardModel;
use App\Modules\Apicrzs\Entities\UsersModel;
use App\Traits\CryptAES;
use http\Params;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UsersServices extends BaseServices
{

    public function __construct(UsersModel $model)
    {
        $this->model = $model;

        $token =  \request()->header('token');
        $user_info = CryptAES::decryptToken($token,'user_grzq');
//
        $this->user_id = $user_info['data']['user_id']??"";
        $this->model = $model;
    }
    /**
     * @param User $info
     * @return string
     */
    public function update(array $params)
    {
        $params['id'] = $this->user_id;
        DB::beginTransaction();
        try{
            parent::update($params); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    public function details(){
        $user_info = $this->model->with(['usersIdCard'])->where('id',$this->user_id)->first();
        $is_verification = $user_info['usersIdCard']['status']==1?true:false;
        $user_info['is_verification'] =$is_verification;

        return [
            'id'=>$user_info['id'],
            'openid'=>$user_info['openid'],
            'nickname'=>$user_info['nickname'],
            'integral'=>$user_info['integral'],
            'mobile'=>$user_info['mobile'],
            'username'=>$user_info['username'],
            'water_role'=>$user_info['water_role'],
            'sharing_ratio'=>$user_info['sharing_ratio'],
            'manage_ratio'=>$user_info['manage_ratio'],
            'is_verification'=>$is_verification
        ];
    }

    /**
     *  绑定手机号
     * @param $params
     * @return bool
     * @throws Exception
     */
    public function binding($params){
        $phone = $params['phone'];
        $captcha = $params['captcha'];
        $verifyCode = Cache::get('code_' . $phone);

        $verifyCode = substr($verifyCode, 0, 6);
        if ($verifyCode != $captcha) {
            $this->error = '验证码错误！';
            return false;
        }
        DB::beginTransaction();
        try{
            parent::update([
                'id'=>$this->user_id,
                'username'=>$phone,
                'mobile'=>$phone
            ]); // TODO: Change the autogenerated stub

            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }

    public function verification($params)
    {
        $user_id =$this->user_id;
        $UsersIdCardModel = UsersIdCardModel::getInstance();
        $card_info = $UsersIdCardModel->where(['user_id'=>$this->user_id])->first();
        DB::beginTransaction();
        try{
            $card = [
                'real_name' => $params['real_name'],
                'id_card_number' => $params['id_card_number'],
                'id_card_front' => $params['id_card_front'],
                'id_card_back' => $params['id_card_back'],
                'status' =>0,
            ];
            $UsersIdCardModel->where(['user_id'=>$user_id])->update($card);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }

    }
}
